<?php
/*由bean脚本生成 powerBy--joe*/

namespace app\common\manage;

use app\common\enum\UserAddressDefaultEnum;
use app\common\validate\UserAddressValidate;
use app\common\model\UserAddressModel;
use app\common\bean\UserAddressBean;
use app\common\bean\ListMap;
use app\common\utils\TimeUtil;
use app\common\exception\MyException;
use Exception;

class UserAddressManage extends BaseManage
{
    /**
     * @return UserAddressManage
     */
    static function getInstance()
    {
        return parent::getInstance();
    }

    /**
     * @desc 获取分页数据
     * @param ListMap $listMap
     * @return ListMap $listMap
     * @throws Exception
     **/
    public function getPaginatorList(ListMap $listMap)
    {
        $conditionMap = new ListMap();
        if ($timeArray = TimeUtil::getInstance()->dealBetweenTime($listMap))
            $conditionMap->setParameter(UserAddressBean::$alias . POINST . UserAddressBean::$createdAt, $timeArray);
        $conditionMap->setAlias(UserAddressBean::$alias);
        $listMap->setField(UserAddressBean::$alias . ALL_FIELD);
        !$this->getParamBool($listMap, "orderFieldName") ?: $conditionMap->setOrder($listMap->getParameter('orderFieldName') . SPACE . $listMap->getParameter('orderType'));
        $conditionMap->setWith('user');
        $conditionMap->setParameter(UserAddressBean::$default, $listMap->getParameter(UserAddressBean::$default));
        $resultMap = UserAddressModel::getInstance()->getList($conditionMap);
        $resultMap ?: MyException::throwMyException("查询数据失败");
        return $resultMap;
    }

    /**
     * @desc 添加数据
     * @param UserAddressBean $userAddressBean
     * @return int int
     * @throws Exception
     **/
    public function add($userAddressBean)
    {
        UserAddressValidate::getInstance()->goCheck(UserAddressValidate::$add, $userAddressBean->getData());
        if ($userAddressBean->getDefault() == UserAddressDefaultEnum::ISDEFAULT)
            UserAddressModel::getInstance()->updatedData([UserAddressBean::$userId => $userAddressBean->getUserId()], [UserAddressBean::$default => UserAddressDefaultEnum::NODEFAULT]);

        $userAddressBean->setCreatedAt(TimeUtil::getInstance()->getTimeNow());
        $result = UserAddressModel::getInstance()->addDataGetInsertId($userAddressBean->getData());
        $result ?: MyException::throwMyException("插入数据失败");
        return $result;
    }

    /**
     * @desc 编辑数据
     * @param UserAddressBean $userAddressBean
     * @return int int
     * @throws Exception
     **/
    public function edit($userAddressBean)
    {
        if ($userAddressBean->getDefault() == UserAddressDefaultEnum::ISDEFAULT)
            UserAddressModel::getInstance()->updatedData([UserAddressBean::$userId => $userAddressBean->getUserId()], [UserAddressBean::$default => UserAddressDefaultEnum::NODEFAULT]);
        $result = UserAddressModel::getInstance()->updatedData([UserAddressBean::$id => $userAddressBean->getId()], $userAddressBean->getData());
        $result ?: MyException::throwMyException("更新失败");
        return $result;
    }

    /**
     * @desc 编辑数据
     * @param UserAddressBean $userAddressBean
     * @return int int
     * @throws Exception
     **/
    public function editAll($userAddressBean)
    {
        $result = UserAddressModel::getInstance()->updatedData([UserAddressBean::$userId => $userAddressBean->getUserId()], $userAddressBean->getData());
        $result ?: MyException::throwMyException("更新失败");
        return $result;
    }

    /**
     * @desc 删除数据
     * @param UserAddressBean $userAddressBean
     * @return int int
     * @throws Exception
     **/
    public function delete($userAddressBean)
    {
        $result = UserAddressModel::getInstance()->deleteData([UserAddressBean::$id => $userAddressBean->getId()]);
        $result ?: MyException::throwMyException("删除失败");
        return $result;
    }

    /**
     * @desc 根据主建获取数据
     * @param UserAddressBean $userAddressBean
     * @return UserAddressBean $userAddressBean
     * @throws Exception
     **/
    public function getInfo($userAddressBean)
    {
        UserAddressValidate::getInstance()->goCheck(UserAddressValidate::$pkId, $userAddressBean->getData());
        // Todo:添加业务处理代码...

        $result = UserAddressModel::getInstance()->selectData([UserAddressBean::$id => $userAddressBean->getId()]);
        $result ?: MyException::throwMyException("获取信息失败");
        $userAddressBean->setData($result);
        return $userAddressBean;
    }

    /**
     * @param UserAddressBean $userAddBean
     * @return mixed
     * @throws Exception
     */
    public function getDefaultAddr(UserAddressBean $userAddBean)
    {
        $result = UserAddressModel::getInstance()->selectData([UserAddressBean::$userId => $userAddBean->getUserId(),UserAddressBean::$default=>UserAddressDefaultEnum::ISDEFAULT]);
        !$result ?: $userAddBean->setData($result);
        return $userAddBean;
    }
}
